/************************************************************************
 * Id: uci_cfg.c                                                        *
 *                                                                      *
 * TR069 Project:  A TR069 library in C                                 *
 * Copyright (C) 2013-2014  netcwmp.netcwmp group                       *
 *                                                                      *
 *                                                                      *
 * Email: netcwmp ( & ) gmail dot com                                   *
 *                                                                      *
 ***********************************************************************/

#include <suci.h>
#include <cwmp/cwmp.h>
#include <cwmp/pool.h>
#include <cwmp/log.h>
#include <cwmp/uci_cfg.h>
#include <ini.h>


int cwmp_uci_get(const char * key, char *value)
{
    int ret = CWMP_ERROR;

    FUNCTION_TRACE();
    if(key == NULL)
    {
        return -1;
    }

	ret = suci_get(key, value);
	
    return ret;
}

int cwmp_uci_set(const char * key, const char * value)
{
    int ret = CWMP_ERROR;
	
    FUNCTION_TRACE();
    if(key == NULL)
    {
        return CWMP_ERROR;
    }

	ret = suci_set(key, value);

    return ret;
}

int cwmp_uci_set_int(const char * key, int value)
{
    int ret = CWMP_ERROR;
	char str_value[64] = {0};
	
    FUNCTION_TRACE();
    if(key == NULL)
    {
        return CWMP_ERROR;
    }

	sprintf(str_value, "%d", value);
	ret = suci_set(key, str_value);

    return ret;
}

int cwmp_uci_set_uint(const char * key, unsigned int value)
{
    int ret = CWMP_ERROR;
	char str_value[64] = {0};
	
    FUNCTION_TRACE();
    if(key == NULL)
    {
        return CWMP_ERROR;
    }

	sprintf(str_value, "%u", value);
	ret = suci_set(key, str_value);

    return ret;
}

char * cwmp_uci_pool_get(pool_t * pool, const char * key)
{
    char value[INI_BUFFERSIZE] = {0};
	int ret = CWMP_ERROR;
	
    //FUNCTION_TRACE();
    if(key == NULL)
    {
        return NULL;
    }

	ret = suci_get(key, value);	

	if(0 != ret)
	{
		return NULL;
	}
	else
	{
	    return pool_pstrdup(pool, value);
	}
}

int cwmp_uci_get_int(const char * key)
{
	char value[INI_BUFFERSIZE] = {0};
	int ret = CWMP_ERROR;
	
    FUNCTION_TRACE();
    if(key == NULL)
    {
        return 0;
    }

	ret = suci_get(key, value);	

	if(0 != ret)
	{
		return 0;
	}
	else
	{
	    return atoi(value);
	}
}

unsigned int cwmp_uci_get_uint(const char * key)
{
	char value[INI_BUFFERSIZE] = {0};
	int ret = CWMP_ERROR;
	
    FUNCTION_TRACE();
    if(key == NULL)
    {
        return 0;
    }

	ret = suci_get(key, value);	

	if(0 != ret)
	{
		return 0;
	}
	else
	{
	    return (unsigned int)atoi(value);
	}
}


